package com.schoolhelper.controller.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.modules.conversation.EaseConversationListFragment;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.schoolhelper.controller.activity.ChatActivity;

import java.util.List;

public class ConversationListFragment extends EaseConversationListFragment {

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        EMClient.getInstance().chatManager().addMessageListener(emMessageListener);
    }

    private EMMessageListener emMessageListener = new EMMessageListener() {
        @Override
        public void onMessageReceived(List<EMMessage> messages) {
            conversationListLayout.refreshList();
        }

        @Override
        public void onCmdMessageReceived(List<EMMessage> messages) {

        }

        @Override
        public void onMessageRead(List<EMMessage> messages) {

        }

        @Override
        public void onMessageDelivered(List<EMMessage> messages) {

        }

        @Override
        public void onMessageRecalled(List<EMMessage> messages) {

        }

        @Override
        public void onMessageChanged(EMMessage message, Object change) {

        }
    };

    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        Object item = conversationListLayout.getItem(position).getInfo();
        if(item instanceof EMConversation) {
            Intent intent = new Intent(getActivity(), ChatActivity.class);
            intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID, ((EMConversation)item).conversationId());
            intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseCommonUtils.getChatType((EMConversation)item));
            intent.putExtra(EaseConstant.EXTRA_IS_ROAM, true);
            startActivity(intent);
        }
    }
}
